package com.lai.gulimall.auth.controller;

import com.lai.common.constant.AuthServerConstant;
import com.lai.common.vo.MemberRespVo;
import com.lai.gulimall.auth.service.Oauth2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class Oauth2Controller {

    @Autowired
    Oauth2Service oauth2Service;


    @GetMapping("/oauth2.0/{platform}/auth")
    public String weibo(@RequestParam("code") String code, @PathVariable("platform") String platform, HttpSession session){
        if(platform.equals("weibo")){
            MemberRespVo weibo = oauth2Service.weibo(code);
            if(weibo != null){
                // TODO 1、默认的session只在当前域中有用  （其他域则没有、 解决子域session共享问题）
                // TODO 2、使用json序列化方式来序列化对象数据到redis中
                session.setAttribute(AuthServerConstant.LOGIN_USER,weibo);
                return "redirect:http://gulimall.com";
            }
        }else if(platform.equals("gitee")){
            MemberRespVo gitee = oauth2Service.gitee(code);
            if(gitee != null){
                session.setAttribute(AuthServerConstant.LOGIN_USER,gitee);
                return "redirect:http://gulimall.com";
            }
        }else if(platform.equals("github")){
            MemberRespVo github = oauth2Service.github(code);
            if(github != null){
                session.setAttribute(AuthServerConstant.LOGIN_USER,github);
                return "redirect:http://gulimall.com";
            }
        }
        //2、登录成功就跳回首页
        return "redirect:http://auth.gulimall.com";
    }
}
